package com.hiyee.goods.repository;

import com.hiyee.goods.mapper.GoodsMapper;
import com.hiyee.goods.mapper.ProductMapper;
import com.hiyee.goods.mapper.SpecMapper;
import com.hiyee.goods.mapper.SpecValueMapper;
import com.hiyee.pojo.Goods;
import com.hiyee.pojo.Product;
import com.hiyee.pojo.SpecValue;
import com.hiyee.pojo.repo.GoodsInfoRepo;
import com.hiyee.pojo.repo.SpecRepo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class GoodsRepo {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private SpecMapper specMapper;
    @Autowired
    private SpecValueMapper specValueMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 根据 商品 id 获取商品详细信息
     *
     * @param Long id
     * @return GoodsInfoRepo
     */
    public GoodsInfoRepo getById(Long id) {
        Goods goods = goodsMapper.getById(id);
        GoodsInfoRepo goodsInfoRepo = new GoodsInfoRepo();
        BeanUtils.copyProperties(goods, goodsInfoRepo);
        // 查询商品规格
        List<SpecRepo> specList = specMapper.getByGoodsId(id);
        // 查询商品规格的规格值
        for (SpecRepo spec : specList) {
            System.out.println(spec.getSpecId());
            List<SpecValue> specValueList = specValueMapper.getBySpecId(spec.getSpecId());
            spec.setValueList(specValueList);
        }
        goodsInfoRepo.setSpecList(specList);
        // 查询规格商品
        goodsInfoRepo.setProducts(productMapper.getByGoodsId(id));
        return goodsInfoRepo;
    }

    /**
     * 根据商品 id 查询商品
     *
     * @param Long productId
     * @return Product
     */
    public Product getProducById(Long productId) {
        return productMapper.getById(productId);
    }
}
